package com.spring.boot.config.security;

import com.spring.boot.config.security.handler.*;
import com.spring.boot.service.impl.MyUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;



@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private MyUserDetailService myUserDetailService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private MyAuthenticFailureHandler myAuthenticFailureHandler;

    @Autowired
    private MyAuthenticSucceedHandler myAuthenticSucceedHandler;

    @Autowired
    private MyLogoutHandler myLogoutHandler;
    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;

    @Autowired
    private MyAuthEntryPoint myAuthEntryPoint;
    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth
                .userDetailsService(myUserDetailService)
                .passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(WebSecurity web) throws Exception{
        super.configure(web);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception{
//        super.configure(http);

        http.formLogin().loginProcessingUrl("/login").permitAll()
//        http.formLogin().loginProcessingUrl("/login").permitAll()
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(myAuthenticSucceedHandler)
                .failureHandler(myAuthenticFailureHandler);
        http.authorizeRequests()
                .antMatchers("/").permitAll()
//                .antMatchers("/user/**").hasAnyAuthority("ROLE_admin")
//                .antMatchers("/admin/**").hasRole("admin")
                .anyRequest().authenticated()
                .and().exceptionHandling().authenticationEntryPoint(myAuthEntryPoint)
                .accessDeniedHandler(myAccessDeniedHandler)
                .and().logout().permitAll()
                .addLogoutHandler(myLogoutHandler)
                .logoutSuccessHandler(myLogoutSuccessHandler)
                .deleteCookies("JSESSIONID")
                .and().csrf().disable();

    }
}
